package com.ycdx.experts.utils;

import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBPool {
	private static DBPool dbPool;
	private ComboPooledDataSource dataSource;
	private final static String USER = "root";
	private final static String PASSWORD = "t2000vip";

	static {
		dbPool = new DBPool();
	}

	public   DBPool(){       
            try   {       
                    dataSource = new ComboPooledDataSource();       
                    dataSource.setUser( USER);       
                    dataSource.setPassword( PASSWORD);       
                    dataSource.setJdbcUrl( "jdbc:mysql://127.0.0.1:3306/expert?autoReconnect=true&useUnicode=true"); 
                    dataSource.setDriverClass( "com.mysql.jdbc.Driver"); 
                    dataSource.setInitialPoolSize(3); 
                    dataSource.setAcquireIncrement(3);
                    dataSource.setAcquireRetryAttempts(30);
                    dataSource.setAcquireRetryDelay(1000);
                    dataSource.setAutoCommitOnClose(false);
                    dataSource.setAutomaticTestTable("Test");
                    dataSource.setCheckoutTimeout(0);
                    dataSource.setForceIgnoreUnresolvedTransactions(false);
                    dataSource.setNumHelperThreads(3);
                    dataSource.setIdleConnectionTestPeriod(60);
                    dataSource.setMinPoolSize(1); 
                    dataSource.setMaxPoolSize(40); 
                    dataSource.setMaxStatements(100); 
                    dataSource.setMaxIdleTime(60);       
            }   catch   (PropertyVetoException   e)   {       
                throw   new   RuntimeException(e);       
            }       
    }	
	
	public final static DBPool getInstance() {
		return dbPool;
	}

	public final Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException("无法从数据源获取连接 ", e);
		}
	}
/*
	public static void main(String[] args) throws SQLException {
		Connection con = null;
		try {
			con = DBPool.getInstance().getConnection();
		} catch (Exception e) {
		} finally {
			if (con != null)
				con.close();
		}
	}
	*/
}